//
//  CSMappingManager.m
//  Pods
//
//  Created by SoulFireWang on 17/4/8.
//
//

#import "CSPageManager.h"
#import "CSPageModel.h"
#import "CSPageElementModel.h"
#import "CSPageRequestModel.h"
#import "CSPageParameterModel.h"

@interface CSPageManager()

@property (nonatomic, strong) NSMutableArray *jsonViewArray;

@property (nonatomic, strong) UIView *pageView;

@property (nonatomic, assign) BOOL isNavigationBarVisible;

@property (nonatomic, strong) CSPageModel *pageModel;

@end

@implementation CSPageManager

static CSPageManager *_pageManager;

/**
 * 单例对象
 */
+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _pageManager = [CSPageManager new];
    });
    return _pageManager;
}

/**
 * 实例化一个页面
 */
-(void)parsePage:(NSDictionary *)descriptor{
    
    CSPageModel *pageModel = [CSPageModel new];
    
    pageModel.id = [descriptor objectForKey:@"page_id"];
    pageModel.pageTitle = [descriptor objectForKey:@"page_title"];
    
    //获取页面布局元素
    NSArray *pageElements = [descriptor objectForKey:@"page_elements"];
    for (int i = 0; i < pageElements.count; i++) {
        
        NSDictionary *elementDic = [pageElements objectAtIndex:i];
        CSPageElementModel *pageElementModel = [CSPageElementModel new];
        pageElementModel.type = [[elementDic objectForKey:@"type"] intValue];
        pageElementModel.title = [elementDic objectForKey:@"title"];
        pageElementModel.defaultValue = [elementDic objectForKey:@"defaultValue"];
        pageElementModel.json = [elementDic objectForKey:@"json"];
        [pageModel.elements addObject:pageElementModel];
        
    }
    
    
    //获取页面请求元素
    NSArray *pageRequests = [descriptor objectForKey:@"page_requests"];
    for (int i = 0; i < pageRequests.count; i++) {
        
        //获取请求地址
        NSDictionary *requsetDic = [pageRequests objectAtIndex:i];
        CSPageRequestModel *pageRequestModel = [CSPageRequestModel new];
        pageRequestModel.requestUrl = [requsetDic objectForKey:@"url"];
        
        
        //获取请求参数
        NSArray *parameters = [requsetDic objectForKey:@"parameters"];
        if (parameters == nil) {
            continue;
        }
        for (int j = 0; j < parameters.count; j++) {
            
            NSDictionary *parameterDic = [parameters objectAtIndex:j];
            CSPageParameterModel *parameterModel = [CSPageParameterModel new];
            parameterModel.json = [parameterDic objectForKey:@"json"];
            parameterModel.value = [parameterDic objectForKey:@"value"];
            //请求对象添加参数
            [pageRequestModel.parameters addObject:parameterModel];
            
        }
        
        [pageModel.requests addObject:pageRequestModel];
        
    }
    
    self.pageModel = pageModel;
    
}




-(UIView *)getCSPage:(NSArray *)descriptionDatas isNavigationBarVisible:(BOOL)isNavigationBarVisible{
    
    NSArray *csViews = [self getCSViews:descriptionDatas];
    UIView *pageView = [self getPageView:csViews isNavigationBarVisible:isNavigationBarVisible];
    return pageView;
    
}

-(UIView *)getPageView:(NSArray *)viewArray isNavigationBarVisible:(BOOL)isNavigationBarVisible;{
    
    //先移除所有字视图
    for (int i = 0; i < self.pageView.subviews.count; i++) {
        UIView *subView = [self.pageView.subviews objectAtIndex:i];
        [subView removeFromSuperview];
    }
    
    UIView *pageView = self.pageView;
    
    CGFloat primaryY = 0;
    
    if (isNavigationBarVisible) {
        pageView.frame = CGRectMake(0, HEIGHT_NAVIGATIONBAR, kWindowW, HEIGHT_CONTENT);
    }else{
        pageView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
    }
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = pageView.frame;
    [pageView addSubview:scrollView];
    
    for (CSBaseView *csView in viewArray ) {
        
        primaryY = [csView layoutView:scrollView primaryY:primaryY];
        
    }
    
    scrollView.contentSize = CGSizeMake(0, primaryY);
    
    return pageView;
}

-(NSArray *)getCSViews:(NSArray *)descriptionDatas{
    
    NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    
    if (descriptionDatas != nil) {
        
        for (NSArray *description in descriptionDatas) {
            
            if (description.count <= 0) {
                continue;
            }
            
            int type = [[description objectAtIndex:0] intValue];
            
            if (type == CSViewType_PageSectionTitleView) {
                
                //标题栏视图
                NSString *titleString = [description objectAtIndex:1];
                NSString *titleColor = [description objectAtIndex:2];
                NSString *imageUrl = nil;
                if (description.count > 3) {
                    imageUrl = [description objectAtIndex:3];
                }
                
                CS_Page_SectionTitleView *pageSectionTitleView =
                [CS_Page_SectionTitleView page_SectionTitleView:titleString color:[UIColor colorFromHexCode:titleColor] image:nil];
                
                [self.jsonViewArray addObject:pageSectionTitleView];
                
                [resultArray addObject:pageSectionTitleView];
                
            }else if (type == CSViewType_PageTextView){
                
                //文档视图
                NSString *titleString = [description objectAtIndex:1];
                NSString *jsonFieldString = [description objectAtIndex:2];
                NSString *dataValue = nil;
                
                if (description.count > 3) {
                    dataValue = [description objectAtIndex:3];
                }
                
                CS_Page_TextView *pageTextView = [CS_Page_TextView page_textView:titleString dataValue:dataValue];
                pageTextView.jsonField = jsonFieldString;

                [self.jsonViewArray addObject:pageTextView];
                
                [resultArray addObject:pageTextView];
                
            }else if (type == CSViewType_PageImageView){
                
                //图片视图
                NSString *titleString = [description objectAtIndex:1];
                NSString *jsonFieldString = [description objectAtIndex:2];
                NSArray *dataValue = nil;
                
                if (description.count > 3) {
                    dataValue = [description objectAtIndex:3];
                }
                
                CS_Page_ImageView *pageTextView = [CS_Page_ImageView page_ImageView:titleString images:dataValue mark:nil];
                pageTextView.jsonField = jsonFieldString;
                
                [self.jsonViewArray addObject:pageTextView];
                [resultArray addObject:pageTextView];
                
            }
        }
    }
    
    return resultArray;
}

-(UIView *)refreshValue:(id)responseObj{
    
    for (CSBaseView *baseVeiw in self.jsonViewArray) {
        
        if (baseVeiw.jsonField != nil && ![baseVeiw.jsonField isEqualToString:@""]) {
            
            NSString *valueString = (NSString *)[responseObj objectForKey:baseVeiw.jsonField];
            
            if (valueString != nil) {
                baseVeiw.value = valueString;
            }
        }
        
    }
    
   return [self getPageView:self.jsonViewArray isNavigationBarVisible:self.isNavigationBarVisible];
    
}

-(NSMutableArray *)jsonViewArray{
    if (_jsonViewArray == nil) {
        _jsonViewArray = [[NSMutableArray alloc]init];
    }
    return _jsonViewArray;
}

-(void)clearBuffer{
    [self.jsonViewArray removeAllObjects];
}

-(UIView *)pageView{
    if (_pageView == nil) {
        _pageView = [[UIView alloc]init];
    }
    return _pageView;
}

@end
